
import 'package:canvas/canvas/custom_drawer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

class CanvasPaint extends StatefulWidget {
  const CanvasPaint({super.key});

  @override
  State<CanvasPaint> createState() => _CanvasPaintState();
}

class _CanvasPaintState extends State<CanvasPaint> {
   List<DrawPoints> drawPoints = [];
  Color drawColor = Colors.black;
  bool isFirstLog = true;
  @override
  Widget build(BuildContext context) {
    var mouseCursor = SystemMouseCursors.click;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.orange,
        title: const Center(child: Text("rainson king 的画板")),
        actions: [
          IconButton(onPressed: (){
            showSelectColorDialog();
          }, icon: const Icon(Icons.color_lens))
        ],
      ),
      body: MouseRegion(
        cursor: mouseCursor,
        child: GestureDetector(
          // onPanStart: (DragStartDetails dragStartDetails){
          //   debugPrint("CanvasPaint  onPanStart");
          //   setState(() {
          //     RenderBox renderBox = context.findRenderObject() as RenderBox;
          //     final offset = renderBox.globalToLocal(dragStartDetails.localPosition);
          //     if(isFirstLog){
          //       drawPoints.add(DrawPoints(points:[],color:drawColor));
          //       isFirstLog = false;
          //     }
          //     if(drawPoints.isNotEmpty){
          //       drawPoints.last.points.add(offset);
          //     }
          //   });
          // },
          onPanUpdate: (DragUpdateDetails? dragUpdateDetails){
            debugPrint("CanvasPaint  onPanUpdate");
            setState(() {
              Offset? offset = dragUpdateDetails?.localPosition;
              if(isFirstLog){
                drawPoints.add(DrawPoints(points:[],color:drawColor));
                isFirstLog = false;
              }
              if(drawPoints.isNotEmpty){
                drawPoints.last.points.add(offset);
              }
            });
          },
          onPanEnd: (DragEndDetails? dragStartDetails){
            debugPrint("CanvasPaint  onPanEnd");
            setState(() {
              drawPoints.add(DrawPoints(points:[],color:drawColor));
              isFirstLog = true;
            });

          },
          child: CustomPaint(
            painter: CustomDrawer(drawPoints),
            size: Size.infinite,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            drawPoints.clear();
          });
        },
        child: const Icon(Icons.cleaning_services_rounded),
      ),
    );
  }

  void showSelectColorDialog() {
    showDialog(context: context,
        builder: (BuildContext context){
      return AlertDialog(
        title: const Text("选择颜色"),
        content: SingleChildScrollView(
          child: ColorPicker(
            pickerColor: drawColor, onColorChanged: (Color value) {
            setState(() {
              drawColor = value;
            });
            // Navigator.of(context).pop();
          },
          ),
        ),
        actions: [
          TextButton(onPressed: (){
            Navigator.of(context).pop();
          }, child: const Text("确定"))
        ],
      )
      ;
    });
    
  }
}
